// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.network;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Calendar;
import zombie.core.Core;
import zombie.core.logger.ExceptionLogger;
import zombie.core.raknet.UdpConnection;
import zombie.core.znet.SteamUtils;
import zombie.debug.DebugLog;
import zombie.erosion.ErosionMain;
import zombie.gameStates.ChooseGameInfo;
import zombie.gameStates.ConnectToServerState;
import zombie.gameStates.MainScreenState;
import zombie.globalObjects.SGlobalObjects;
import zombie.iso.Vector3;
import zombie.world.WorldDictionary;

public class ConnectionDetails {
    public static void write(UdpConnection connection, ServerWorldDatabase.LogonResult logonResult, ByteBuffer bb) {
        try {
            writeServerDetails(bb, connection, logonResult);
            writeGameMap(bb);
            if (SteamUtils.isSteamModeEnabled()) {
                writeWorkshopItems(bb);
            }

            writeMods(bb);
            writeStartLocation(bb);
            writeServerOptions(bb);
            writeSandboxOptions(bb);
            writeGameTime(bb);
            writeErosionMain(bb);
            writeGlobalObjects(bb);
            writeResetID(bb);
            writeBerries(bb);
            writeWorldDictionary(bb);
        } catch (Throwable throwable) {
            throw new RuntimeException(throwable);
        }
    }

    public static void parse(ByteBuffer b) {
        ConnectionManager.log("receive-packet", "connection-details", null);
        Calendar calendar = Calendar.getInstance();
        ConnectToServerState connectToServerState = new ConnectToServerState(b);
        connectToServerState.enter();
        MainScreenState.getInstance().setConnectToServerState(connectToServerState);
        DebugLog.General.println("LOGGED INTO : %d millisecond", calendar.getTimeInMillis() - GameClient.startAuth.getTimeInMillis());
    }

    private static void writeServerDetails(ByteBuffer byteBuffer, UdpConnection udpConnection, ServerWorldDatabase.LogonResult logonResult) {
        byteBuffer.put((byte)(udpConnection.isCoopHost ? 1 : 0));
        byteBuffer.putInt(ServerOptions.getInstance().getMaxPlayers());
        if (SteamUtils.isSteamModeEnabled() && CoopSlave.instance != null && !udpConnection.isCoopHost) {
            byteBuffer.put((byte)1);
            byteBuffer.putLong(CoopSlave.instance.hostSteamID);
            zombie.GameWindow.WriteString(byteBuffer, GameServer.ServerName);
        } else {
            byteBuffer.put((byte)0);
        }

        int _int = udpConnection.playerIDs[0] / 4;
        byteBuffer.put((byte)_int);
        zombie.GameWindow.WriteString(byteBuffer, logonResult.accessLevel);
    }

    private static void writeGameMap(ByteBuffer byteBuffer) {
        zombie.GameWindow.WriteString(byteBuffer, GameServer.GameMap);
    }

    private static void writeWorkshopItems(ByteBuffer byteBuffer) {
        byteBuffer.putShort((short)GameServer.WorkshopItems.size());

        for (int _int = 0; _int < GameServer.WorkshopItems.size(); _int++) {
            byteBuffer.putLong((Long)GameServer.WorkshopItems.get(_int));
            byteBuffer.putLong(GameServer.WorkshopTimeStamps[_int]);
        }
    }

    private static void writeMods(ByteBuffer byteBuffer) {
        ArrayList arrayList = new ArrayList();

        for (String string0 : GameServer.ServerMods) {
            String string1 = zombie.ZomboidFileSystem.instance.getModDir(string0);
            ChooseGameInfo.Mod mod0;
            if (string1 != null) {
                try {
                    mod0 = ChooseGameInfo.readModInfo(string1);
                } catch (Exception exception) {
                    ExceptionLogger.logException(exception);
                    mod0 = new ChooseGameInfo.Mod(string0);
                    mod0.setId(string0);
                    mod0.setName(string0);
                }
            } else {
                mod0 = new ChooseGameInfo.Mod(string0);
                mod0.setId(string0);
                mod0.setName(string0);
            }

            arrayList.add(mod0);
        }

        byteBuffer.putInt(arrayList.size());

        for (ChooseGameInfo.Mod mod1 : arrayList) {
            zombie.GameWindow.WriteString(byteBuffer, mod1.getId());
            zombie.GameWindow.WriteString(byteBuffer, mod1.getUrl());
            zombie.GameWindow.WriteString(byteBuffer, mod1.getName());
        }
    }

    private static void writeStartLocation(ByteBuffer byteBuffer) {
        Object object = null;
        Vector3 vector3 = ServerMap.instance.getStartLocation((ServerWorldDatabase.LogonResult)object);
        byteBuffer.putInt((int)vector3.x);
        byteBuffer.putInt((int)vector3.y);
        byteBuffer.putInt((int)vector3.z);
    }

    private static void writeServerOptions(ByteBuffer byteBuffer) {
        byteBuffer.putInt(ServerOptions.instance.getPublicOptions().size());

        for (String string : ServerOptions.instance.getPublicOptions()) {
            zombie.GameWindow.WriteString(byteBuffer, string);
            zombie.GameWindow.WriteString(byteBuffer, ServerOptions.instance.getOption(string));
        }
    }

    private static void writeSandboxOptions(ByteBuffer byteBuffer) throws IOException {
        zombie.SandboxOptions.instance.save(byteBuffer);
    }

    private static void writeGameTime(ByteBuffer byteBuffer) throws IOException {
        zombie.GameTime.getInstance().saveToPacket(byteBuffer);
    }

    private static void writeErosionMain(ByteBuffer byteBuffer) {
        ErosionMain.getInstance().getConfig().save(byteBuffer);
    }

    private static void writeGlobalObjects(ByteBuffer byteBuffer) throws IOException {
        SGlobalObjects.saveInitialStateForClient(byteBuffer);
    }

    private static void writeResetID(ByteBuffer byteBuffer) {
        byteBuffer.putInt(GameServer.ResetID);
    }

    private static void writeBerries(ByteBuffer byteBuffer) {
        zombie.GameWindow.WriteString(byteBuffer, Core.getInstance().getPoisonousBerry());
        zombie.GameWindow.WriteString(byteBuffer, Core.getInstance().getPoisonousMushroom());
    }

    private static void writeWorldDictionary(ByteBuffer byteBuffer) throws IOException {
        WorldDictionary.saveDataForClient(byteBuffer);
    }
}
